package com.itheima.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Bean.Category;
import com.itheima.Bean.Dish;
import com.itheima.Bean.DishFlavor;
import com.itheima.Service.CategoryService;
import com.itheima.Service.DishFlavorService;
import com.itheima.Service.DishService;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.cldr.twq.LocaleNames_twq;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/status/{status}")
    public R<String> setStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
//        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
//        lqw.in(Dish::getId,ids);
//        List<Dish> dishList = dishService.list(lqw);
//        for (Dish dish : dishList) {
//            dish.setStatus(status);
//        }
//        dishService.updateBatchById(dishList);
        LambdaUpdateWrapper<Dish> luw = new LambdaUpdateWrapper<>();
        luw.in(Dish::getId,ids);
        luw.set(Dish::getStatus,status);
        dishService.update(luw);
        return R.success("修改状态成功！");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.remove(ids);
        return R.success("删除菜品成功！");
    }

    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto){
        dishService.addDishWithFlavors(dishDto);
        return R.success("新增菜品成功！");
    }


    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Dish::getName,name);
        dishService.page(dishPage,lqw);

        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<DishDto> dishDtoList = new ArrayList<>();
        List<Dish> dishList = dishPage.getRecords();
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        }
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> getDishWithFlavors(@PathVariable Long id){
        return R.success(dishService.getDishWithFlavors(id));
    }

    @PutMapping
    public R<String> updateWithFlavors(@RequestBody DishDto dishDto){
        dishService.updateWithFlavors(dishDto);
        return R.success("修改菜品成功！");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        dlqw.like(dish.getName()!=null,Dish::getName,dish.getName());
        dlqw.eq(Dish::getStatus,1);
        dlqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(dlqw);

        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish d : dishList) {
            DishDto dd = new DishDto();
            BeanUtils.copyProperties(d,dd);

            LambdaQueryWrapper<DishFlavor> dflqw = new LambdaQueryWrapper<>();
            dflqw.eq(DishFlavor::getDishId,d.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dflqw);
            dd.setFlavors(dishFlavorList);

            dishDtoList.add(dd);
        }
        return R.success(dishDtoList);
    }
}
